package me.zhengjie.business.service.impl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.business.repository.ZhsqRepository;
import me.zhengjie.business.service.ZhsqFpService;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.utils.DateTimeUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.StoredProcedureQuery;

/**
 /**
 * @description 复评
 * @author xiahongbo
 * @date 2025-03-25
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class ZhsqFpServiceImpl implements ZhsqFpService {

    private final ZhsqRepository zhsqRepository;
    @PersistenceContext
    private EntityManager em;

    @Override
    public String getFpTargetDate(Long zhsqId, int day, String start) {
        log.info("[ZhsqFpServiceImpl - getFpTargetDate]--------------- PRC_WORKDAY - 开始");
        log.info("[ZhsqFpServiceImpl - getFpTargetDate]--------------- PRC_WORKDAY - day:{}，start:{}，zhsqId:{}", day, start, zhsqId);
        log.info("[ZhsqFpServiceImpl - getFpTargetDate]--------------- PRC_WORKDAY - 执行 PRC_WORKDAY");

        String pgsj = zhsqRepository.getFpTargetDate(zhsqId);
        if(StringUtils.isEmpty(pgsj)) {
            throw new BadRequestException("评估时间为空，无法复评");
        }
        log.info("[ZhsqFpServiceImpl - getFpTargetDate]--------------- PRC_WORKDAY - 评估时间:{}", pgsj);
        String startDate = StringUtils.isEmpty(start) ? DateTimeUtil.getDateTime(DateTimeUtil.addDays(DateTimeUtil.parse(pgsj, DateTimeUtil.DEFAULT_DATE_FORMAT), 5), DateTimeUtil.DEFAULT_DATE_FORMAT) : start;
        log.info("[ZhsqFpServiceImpl - getFpTargetDate]--------------- PRC_WORKDAY - 评估结论:{}", startDate);

        StoredProcedureQuery storedProcedureQuery = this.em.createNamedStoredProcedureQuery("workDay");

        storedProcedureQuery.setParameter("IR_DAY", day);
        storedProcedureQuery.setParameter("IR_START", startDate);

        storedProcedureQuery.execute();
        int code = (int) storedProcedureQuery.getOutputParameterValue("OR_APPCODE");
        String msg = (String) storedProcedureQuery.getOutputParameterValue("OR_MESSAGE");

        log.info("[ZhsqFpServiceImpl - getFpTargetDate]--------------- PRC_WORKDAY - 结束 :code:{}，msg:{}", code, msg);
        if(code == -1) {
            throw new BadRequestException(msg);
        }

        return msg;
    }

    @Override
    public String exeFp(Long zhsqId) {
        log.info("[ZhsqFpServiceImpl - exeFp]--------------- PRC_MAKE_RE_EVALUATION - 开始");
        log.info("[ZhsqFpServiceImpl - exeFp]--------------- PRC_MAKE_RE_EVALUATION - zhsqId:{}", zhsqId);
        log.info("[ZhsqFpServiceImpl - exeFp]--------------- PRC_MAKE_RE_EVALUATION - 执行 PRC_WORKDAY");

        StoredProcedureQuery storedProcedureQuery = this.em.createNamedStoredProcedureQuery("makeReEvaluation");

        storedProcedureQuery.setParameter("IR_ZHID", zhsqId);

        storedProcedureQuery.execute();
        int code = (int) storedProcedureQuery.getOutputParameterValue("OR_APPCODE");
        String msg = (String) storedProcedureQuery.getOutputParameterValue("OR_MESSAGE");

        log.info("[ZhsqFpServiceImpl - exeFp]--------------- PRC_MAKE_RE_EVALUATION - 结束 :code:{}，msg:{}", code, msg);
        if(code == -1) {
            throw new BadRequestException(msg);
        }

        return msg;
    }
}
